package com.ejlchina.tool.singleton

import com.ejlchina.tool.bean.BleDeviceBean
import com.ejlchina.tool.bean.IndicatorDataBean
import com.ejlchina.tool.bean.IndicatorDeviceDataBean
import com.ejlchina.tool.bean.IndicatorResultBean
import com.huyuhui.fastble.data.BleDevice
import com.huyuhui.fastble.exception.BleException
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow

object GlobalAction {
    val notifyResultFlow = MutableSharedFlow<IndicatorResultBean>(0,1, BufferOverflow.SUSPEND)
    val notifyDataFlow = MutableSharedFlow<IndicatorDeviceDataBean>(0,1,BufferOverflow.SUSPEND)
    val startScanIfSuccessFlow = MutableSharedFlow<Boolean>(0,1,BufferOverflow.SUSPEND)
    val scanDeviceResultFlow = MutableSharedFlow<BleDevice>(0,1,BufferOverflow.SUSPEND)
    val scanDeviceFinishFlow = MutableSharedFlow<Boolean>(0,1,BufferOverflow.SUSPEND) //扫描完成Flow
    val mtuSetSuccessFlow = MutableSharedFlow<BleDevice>(0,1,BufferOverflow.SUSPEND)
    val mtuSetFailureFlow = MutableSharedFlow<BleException>(0,1,BufferOverflow.SUSPEND)
    val connectFailureFlow = MutableSharedFlow<BleException>(0,1,BufferOverflow.SUSPEND)
    val connectSuccessFlow = MutableSharedFlow<BleDeviceBean>(0,1,BufferOverflow.SUSPEND)
    val disConnectFlow = MutableSharedFlow<BleDevice>(0,10,BufferOverflow.SUSPEND)


    val writeSuccessFlow = MutableSharedFlow<IndicatorDataBean>(0,500,BufferOverflow.SUSPEND)
    val writeFailureFlow = MutableSharedFlow<BleWriteException>(0,500,BufferOverflow.SUSPEND)

    val firmwareUpgradeMap = mutableMapOf<String,MutableSharedFlow<Boolean>>()

    fun getFirmwareUpdateFlows(){
        firmwareUpgradeMap.clear()
        GlobalParams.firmwareUpgradeList.filter { it.upgradeResult == 0F }.forEach {
            firmwareUpgradeMap[it.bleDevice.mac] = MutableSharedFlow<Boolean>(1,1,BufferOverflow.DROP_OLDEST)
        }
    }
}

data class BleWriteException(val bleDevice: BleDevice, val bleException: BleException)